#include "EasyBMP.h"

#include <iostream>

using namespace std;

int main( int argc, char* argv[] )
{
 if( argc < 3 )
 { return -1; }

 BMP Image;
 Image.ReadFromFile( argv[1] );
 
 int factor;
 
 char factor_string [1024];
 cout << "Factor: ";
 cin >> factor_string;
 factor = atoi( factor_string );

 char threshold_string [1024];
 cout << "Threshold: ";
 cin >> threshold_string;
 int threshold = atoi( threshold_string );
 
 for( int j=0; j < Image.TellHeight() ; j++ )
 {
  for( int i=0; i < Image.TellWidth() ; i++ )
  {
   int temp = (int) Image(i,j)->Red + 1;
   int divided = temp / factor;
   int remainder = temp - factor*divided;
   
   if( remainder >= factor/2 )
   { divided += 1; }
   
   divided *= factor;
   
   if( divided > 256-threshold*0 )
   { divided = 256; }
   if( divided < 1+threshold )
   { divided = 1; }
   
   divided--;
   
   ebmpBYTE temp_byte = (ebmpBYTE) divided;

   Image(i,j)->Red = temp_byte;
   Image(i,j)->Green = temp_byte;
   Image(i,j)->Blue = temp_byte; 
 
  }
 }
 
 Image.WriteToFile( argv[2] );

 return 0;
}
